package com.example.hospitalmanagement.activity

import android.annotation.SuppressLint
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.ImageView
import android.widget.PopupWindow
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.hospitalmanagement.R
import com.example.hospitalmanagement.adapter.DoctorPopupWindowAdapter
import com.example.hospitalmanagement.adapter.ExpertIntroductionActivityAdapter
import com.example.hospitalmanagement.bean.DepartmentListData
import com.example.hospitalmanagement.bean.ExpertIntroductionData
import com.example.hospitalmanagement.viewmodel.HospitalViewModel

/**
 * 专家介绍Activity
 *
 * 位置:医院综合-专家介绍
 */
class ExpertIntroductionActivity : AppCompatActivity() {
    private lateinit var hospitalViewModel:HospitalViewModel
    private lateinit var downArrow: ImageView
    private lateinit var popupWindowDataList:MutableList<DepartmentListData>
    private lateinit var deskWork: MutableMap<String,Array<Int>>
    private lateinit var map:MutableMap<String,String>
    private lateinit var dataList: MutableList<ExpertIntroductionData>
    private lateinit var expertIntroductionAdapter:ExpertIntroductionActivityAdapter
    private lateinit var deskWorkArray:MutableList<Int>
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_expert_introduction)
        initView()
    }

    private fun initView() {
        hospitalViewModel=ViewModelProviders.of(this).get(HospitalViewModel::class.java)
        val doctorItem: RecyclerView = findViewById(R.id.expert_information_item)
        doctorItem.layoutManager = LinearLayoutManager(this)
        dataList = mutableListOf()
        expertIntroductionAdapter = ExpertIntroductionActivityAdapter(this)
        map = mutableMapOf()
        map["pageSize"]="10"
        map["pageNum"]="1"
        deskWork = mutableMapOf()
        deskWorkArray= mutableListOf()
        hospitalViewModel.getExpertIntroduction(map,deskWork){
            dataList.addAll(it)
            expertIntroductionAdapter.notifyDataSetChanged()
        }
        expertIntroductionAdapter.dataList = dataList
        doctorItem.adapter = expertIntroductionAdapter
    }

    /**
     * 弹窗显示
     */
    @SuppressLint("InflateParams")
    private fun showPopupWindow() {
        val viewPopupWindow = LayoutInflater.from(this).inflate(R.layout.doctor_popup_window, null)
        val doctorItem: RecyclerView = viewPopupWindow.findViewById(R.id.doctor_popup_window_item)
        doctorItem.layoutManager = LinearLayoutManager(this)
        val adapter = DoctorPopupWindowAdapter(this)
        popupWindowDataList= mutableListOf()
        hospitalViewModel.getDepartmentList {
            popupWindowDataList.addAll(it)
            adapter.notifyDataSetChanged()
        }
        adapter.dataList = popupWindowDataList
        doctorItem.adapter = adapter
        val popupWindow = PopupWindow(this)
        val width = windowManager.defaultDisplay.height;
        popupWindow.width = width / 4
        popupWindow.height = ViewGroup.LayoutParams.MATCH_PARENT
        popupWindow.contentView = viewPopupWindow
        popupWindow.setBackgroundDrawable(ColorDrawable(getColor(R.color.white)))
        popupWindow.isOutsideTouchable = false
        popupWindow.isFocusable = true
        popupWindow.showAsDropDown(downArrow, -500, 0, Gravity.LEFT)
        darkenBackground(0.2F)
        popupWindow.setOnDismissListener {
            darkenBackground(1F)
        }
        adapter.setListener {
            popupWindowDataList[it].checked = !popupWindowDataList[it].checked
            if (popupWindowDataList[it].checked){
                deskWorkArray.add(popupWindowDataList[it].deskId)
                deskWork["array"]=deskWorkArray.toTypedArray()
            }
            if (deskWorkArray.size==0){
                deskWork.clear()
            }
            map["pageSize"]="10"
            map["pageNum"]="1"
            hospitalViewModel.getExpertIntroduction(map,deskWork){data->
                dataList.addAll(data)
            }
            adapter.notifyDataSetChanged()
            expertIntroductionAdapter.notifyDataSetChanged()
        }
    }

    /**
     * 当Activity加载完成后显示弹窗
     */
    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (hasFocus) {
            downArrow = findViewById(R.id.down_arrow)
            downArrow.setOnClickListener {
                showPopupWindow()
            }
        }
    }

    /**
     * 弹窗空白部分背景设置
     */
    private fun darkenBackground(bgColor: Float) {
        val attribute = window.attributes
        attribute.alpha = bgColor

        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
        window.attributes = attribute
    }
}